<template>
	<view class="coupon-container">
		<!-- 顶部导航
    <view class="nav-bar">
      <view class="nav-back" @click="goBack">&#8592;</view>
      <view class="nav-title">优惠券</view>
    </view> -->

		<!-- 选项卡 -->
		<view class="tab-container">
			<view class="tab-item" :class="{ active: activeTab === 0 }" @click="switchTab(0)">未使用</view>
			<view class="tab-item" :class="{ active: activeTab === 1 }" @click="switchTab(1)">已使用</view>
			<view class="tab-item" :class="{ active: activeTab === 2 }" @click="switchTab(2)">已过期</view>
		</view>

		<!-- 优惠券列表 -->
		<view v-if="unusedCoupons.length > 0" class="coupon-list">
			<view v-for="(item, index) in unusedCoupons" :key="index">
				<view class="coupon-item" :class="{ 'un-used': item.status == 0, used: item.status == 1, expired: item.status == 2 }">
					<view class="coupon-left">
						<view class="coupon-value">
							<text class="coupon-flats">￥</text>
							{{ parseInt(item.amount) }}
						</view>
						<view class="coupon-unit">优惠券</view>
					</view>
					<view class="coupon-right">
						<view class="coupon-title">{{ item.title }}</view>
						<view class="coupon-date">有效期：{{ item.days }}日</view>
						<view class="coupon-footer">
							<view class="coupon-rules" @click="toggleRules(index)">
								<text>使用规则</text>
								<image v-if="!showRules[index]" class="gray-detail-icon" src="/static/personalCenter/gray-right.png" mode="aspectFill"></image>
								<image
									v-if="showRules[index]"
									class="gray-detail-icon"
									src="/static/personalCenter/gray-right.png"
									mode="aspectFill"
									style="transform: rotate(-90deg); transform-origin: 70% 50%"
								></image>
							</view>
							<view class="coupon-use-btn" :class="{ 'coupon-status': item.status != 0 }">{{ btnText[item.status] }}</view>
						</view>
					</view>
				</view>
				<view v-if="showRules[index]" class="rules-details-warps">
					<view class="rules-shape">
						<text class="rules-shape-item"></text>
						<text class="rules-shape-item"></text>
					</view>
					<view class="rules-content">
						<view class="rules-content-warps">
							<view class="rule-title">使用规则：</view>
							<!-- 		<view class="rule-item">1、优惠券可降低产品的价格，是一种常见的消费者营业推广工具。</view>
							<view class="rule-item">2、优惠券可以印在杂志的插页上</view>
							<view class="rule-item">3、或夹在报纸中随附送</view> -->
							<view class="rule-item" v-html="rule"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<empty-data v-else defaultText="暂无数据" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			activeTab: 0, // 当前选中的tab
			currentActiveIdex: -1,
			showRules: {}, // 控制规则显示
			unusedCoupons: [], // 优惠券数据
			btnText: {
				0: '去使用',
				1: '已使用',
				2: '已过期'
			},
			rule: ''
		};
	},
	onLoad() {
		this.getCouponsList();

		this.$http.get('/h5/common/configByCode?code=coupon_use_rule').then((res) => {
			console.log('*********************>', res);
			this.rule = res.data[0].value;
		});
	},
	methods: {
		async getCouponsList() {
			let params = {
				status: this.activeTab
			};
			try {
				const data = await this.$http.get('/h5/user/coupons', params);
				this.unusedCoupons = data.data || [];
			} catch (error) {}
		},
		goBack() {
			uni.navigateBack();
		},
		switchTab(index) {
			this.activeTab = index;
			this.getCouponsList();
		},
		toggleRules(index) {
			this.$set(this.showRules, index, !this.showRules[index]);
		}
	}
};
</script>

<style>
/* 导航栏样式 */
.nav-bar {
	display: flex;
	align-items: center;
	height: 88rpx;
	padding: 0 30rpx;
	background-color: #fff;
	position: relative;
	border-bottom: 1rpx solid #eee;
}

.nav-back {
	font-size: 36rpx;
	margin-right: 20rpx;
}

.nav-title {
	font-size: 34rpx;
	font-weight: bold;
	color: #333;
}

/* 选项卡样式 */
.tab-container {
	display: flex;
	background-color: #fff;
	height: 100rpx;
}

.tab-item {
	flex: 1;
	display: flex;
	justify-content: center;
	align-items: center;
	font-size: 32rpx;
	color: #999999;
	position: relative;
}

.tab-item.active {
	color: #ff7937;
	font-weight: 500;
}

.tab-item.active:after {
	content: '';
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	width: 80rpx;
	height: 8rpx;
	background-color: #ff7937;
	border-radius: 16rpx;
}

/* 优惠券列表样式 */
.coupon-list {
	padding: 40rpx 26rpx 0 26rpx;
}

.coupon-item {
	width: 100%;
	display: flex;
	margin-bottom: 20rpx;
	overflow: hidden;
	position: relative;
}

.coupon-left {
	width: 240rpx;
	text-align: center;
	padding-top: 38rpx;
}

.coupon-item.un-used {
	background-image: url('~@/static/personalCenter/coupon-1.png');
	background-size: 100% 100%;
	background-position: 50% 50%;
	background-repeat: no-repeat;
}

.coupon-item.used {
	background-image: url('~@/static/personalCenter/coupon-2.png');
	background-size: 100% 100%;
	background-position: 50% 50%;
	background-repeat: no-repeat;
}

.coupon-item.expired {
	background-image: url('~@/static/personalCenter/coupon-2.png');
	background-size: 100% 100%;
	background-position: 50% 50%;
	background-repeat: no-repeat;
}

.coupon-value {
	font-family: DIN Pro, DIN Pro;
	font-weight: 700;
	font-size: 96rpx;
	color: #ffffff;
	line-height: 42rpx;
	padding-top: 38rpx;
	padding-bottom: 12rpx;
}

.coupon-flats {
	font-size: 32rpx;
}

.coupon-unit {
	font-family: Source Han Sans, Source Han Sans;
	font-size: 28rpx;
	color: #ffffff;
	line-height: 40rpx;
}

.coupon-right {
	flex: 1;
	padding: 26rpx 20rpx 22rpx 44rpx;
}

.coupon-title {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 700;
	font-size: 34rpx;
	color: #0b1f20;
	line-height: 52rpx;
	letter-spacing: 1px;
	margin-bottom: 10rpx;
}

.coupon-date {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-size: 24rpx;
	color: #999999;
	line-height: 36rpx;
	margin-bottom: 20rpx;
}

.coupon-footer {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.coupon-rules {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 400;
	font-size: 24rpx;
	color: #697592;
	line-height: 36rpx;
	display: flex;
	align-items: center;
}

.gray-detail-icon {
	width: 28rpx;
	height: 28rpx;
}

.coupon-use-btn {
	width: 200rpx;
	height: 64rpx;
	line-height: 64rpx;
	background: linear-gradient(90deg, #ff3f39 0%, #ff7937 100%);
	border-radius: 36rpx;
	color: #ffffff;
	font-weight: 500;
	font-size: 28rpx;
	text-align: center;
}

.coupon-status {
	width: 200rpx;
	height: 64rpx;
	line-height: 64rpx;
	background: #f5f5f6;
	border-radius: 36rpx;
	color: #666666;
	font-weight: 500;
	font-size: 28rpx;
	text-align: center;
}

.rules-content {
	padding: 8rpx;
	background: #ffffff;
	border-radius: 16rpx;
	/* position: absolute;
  left: 0rpx;
  bottom: 0; */
	margin-bottom: 20rpx;
}

.rules-content-warps {
	border-radius: 16rpx;
	border: 2rpx dashed #ff7937;
	padding: 20rpx 0 20rpx 20rpx;
}

.rule-title {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-weight: 500;
	font-size: 28rpx;
	color: #0b1f20;
	line-height: 42rpx;
	padding-bottom: 12rpx;
}

.rule-item {
	font-family: Source Han Sans CN, Source Han Sans CN;
	font-size: 26rpx;
	color: #697592;
	line-height: 40rpx;
}

.rule-item:last-child {
	/* margin-bottom: 0; */
}

.rules-details-warps {
	margin-top: -20rpx;
}

.rules-shape {
	display: flex;
	justify-content: space-between;
	padding: 0 40rpx;
}

.rules-shape-item {
	width: 12rpx;
	height: 20rpx;
	background: #ff7937;
	display: block;
}
</style>